﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Clipping.Infrastructure.Models;
using System.Collections.ObjectModel;

namespace ClippingEditor
{
    class OptionalFieldViewModel : GalaSoft.MvvmLight.ViewModelBase
    {
        public OptionalFieldViewModel(OptionalField field, OptionalField.Option value)
        {
            this.Field = field;
            this.Value = value;
        }


        public OptionalField Field
        {
            get;
            set;
        }


        private OptionalField.Option _value;

        public OptionalField.Option Value
        {
            get { return _value; }
            set 
            {
                if (value == this._value)
                    return;
                _value = value;
                RaisePropertyChanged(() => this.Value);
            }
        }
    }

    class MultiSelectOptionalFieldViewModel : GalaSoft.MvvmLight.ViewModelBase
    {
        public MultiSelectOptionalFieldViewModel(OptionalField field, List<OptionalField.Option> value)
        {
            this.Field = field;
            ObservableCollection<OptionViewModel> options=new ObservableCollection<OptionViewModel> ();
            foreach (var op in field.Options)
            {
                OptionViewModel vm = new OptionViewModel() { Option = op };
                options.Add(vm);
            }
            this.Options = options;
            this.Value = value;
        }


        public OptionalField Field
        {
            get;
            set;
        }

   
        public List<OptionalField.Option> Value
        {
            get
            {
                if (Options == null)
                    return null;
                else
                {
                    return Options.Where(op => op.IsSelected).Select(op => op.Option).ToList();
                }
            }
            set
            {
                if (Options == null)
                    return;
                if (value != null)
                {
                    foreach (var opvm in Options)
                    {
                        if (value.Contains(opvm.Option))
                            opvm.IsSelected = true;
                        else
                            opvm.IsSelected = false;
                    }
                }
            }
        }


        public ObservableCollection<OptionViewModel> Options { get; set; }


        public class OptionViewModel : GalaSoft.MvvmLight.ViewModelBase
        {
            public OptionalField.Option Option { get; set; }

            private bool _isSelected;

            public bool IsSelected
            {
                get { return _isSelected; }
                set 
                {
                    if (value != this._isSelected)
                    {
                        RaisePropertyChanged(() => this.IsSelected);
                        _isSelected = value;
                    }
                }
            }
        }
    }
}
